using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.Generator.Common.Forms
{
  public partial class ErrorControl : ListView
  {
    public ErrorControl()
    {
      InitializeComponent();

      System.Type[] typeList = new System.Type[] { typeof(MessageTypeConstants), typeof(int), typeof(string), typeof(string) };
      this.ListViewItemSorter = new ListViewComparer(this, typeList);

      this.Columns.Clear();
      this.Columns.Add("", 16, HorizontalAlignment.Left);
      this.Columns.Add("", 24, HorizontalAlignment.Right);
      this.Columns.Add("Description", 300, HorizontalAlignment.Left);
      this.Columns.Add("Object Type", 300, HorizontalAlignment.Left);

      this.Sorting = SortOrder.Ascending;
      this.SmallImageList = this._imageList;
      this.View = View.Details;
      this.HideSelection = false;
      this.FullRowSelect = true;
    }

    #region Class Members

    protected Image _iconError = null;
    protected Image _iconWarning = null;
    protected Widgetsphere.Generator.Common.GeneratorFramework.Message _message = null;
    private ImageList _imageList = new ImageList();

    #endregion

    #region Property Implementations

    public Image IconError
    {
      get { return _iconError; }
      set
      {
        _iconError = value;
        this.SetupImageList();
      }
    }

    public Image IconWarning
    {
      get { return _iconWarning; }
      set
      {
        _iconWarning = value;
        this.SetupImageList();
      }
    }

    public Widgetsphere.Generator.Common.GeneratorFramework.Message SelectedMessage
    {
      get { return _message; }
      set { _message = value; }
    }

    #endregion

    #region Methods

    private void SetupImageList()
    {
      _imageList.Images.Clear();

      if (this.IconWarning == null)
        _imageList.Images.Add(this.GetIcon("Default.ico"));
      else
        _imageList.Images.Add(this.IconWarning);

      if (this.IconError == null)
        _imageList.Images.Add(this.GetIcon("Default.ico"));
      else
        _imageList.Images.Add(this.IconError);

    }

    public void ClearMessages()
    {
      this.Items.Clear();
    }

    public void AddMessages(MessageCollection messageCollection)
    {
      foreach (Widgetsphere.Generator.Common.GeneratorFramework.Message message in messageCollection)
      {
        this.AddMessage(message);
      }
    }

    public void AddMessage(Widgetsphere.Generator.Common.GeneratorFramework.Message message)
    {
      int ii = 1;
      ListViewItem newItem = new ListViewItem();
      newItem.Tag = message;

      //Image
      if(message.MessageType == MessageTypeConstants.Error)
        newItem.ImageIndex = 1;
      else
        newItem.ImageIndex = 2;

      //Error Number
      newItem.SubItems.Add(ii.ToString());
      ii++;

      //Message
      newItem.SubItems.Add(message.Text);

      //Type Column
      if(message.Controller != null)
      {
        string typeName = message.Controller.Object.GetType().ToString();
        string[] typeNameArr = typeName.Split('.');
        typeName = typeNameArr[typeNameArr.Length - 1];
        newItem.SubItems.Add(typeName);
      }
      else
      {
        newItem.SubItems.Add("");
      }

      //Add to the list
      this.Items.Add(newItem);
    }

    private Icon GetIcon(string iconName)
    {
      return new Icon(GetProjectFileAsStream(iconName));
    }

    private System.IO.Stream GetProjectFileAsStream(string iconName)
    {
      try
      {
        System.Reflection.Assembly asbly = System.Reflection.Assembly.GetExecutingAssembly();
				string name = asbly.GetName().Name;
				name = name.Replace("2008", "");
				System.IO.Stream stream = asbly.GetManifestResourceStream(name + ".Images." + iconName);
        System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.UTF8);
        return sr.BaseStream;
      }
      catch (Exception ex)
      {
        throw;
      }
    }

    #endregion

  }
}